"""
Case Type   : dolphin插件
Case Name   : 验证mysql侧xor操作符
Create At   : 2022.04.06
Owner       : z@wan005
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、验证xor操作符
    4、验证xor操作符,含null值
    5、清理环境
Expect      :
    1、成功
    2、自动加载成功,无需手动创建
    3、成功
    4、成功，返回null
    5、清理环境完成
History     :modified by z@wan005,2022.08.29，om数据库目前实现自动加载dolphin插件，
             因此删除用例中拷贝插件依赖文件步骤
             modified by z@wan005,2022.12.01，sql_mode参数命名变更，修改用例
             modified by z@wan005,2023.10.08，步骤4，select场景不区分严格模式，
             修改断言
              modified by zhanghuan96,2024.7.3，报错提示变更
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class PluginTest013(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_plugin0013'

    def test_dolphin(self):
        text = '----step1:创建b库;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} DBCOMPATIBILITY ='b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '---step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建---'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual('dolphin', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = '-----step3:验证xor操作符;expect:成功---'
        self.log.info(text)
        sql_cmd = f'''set dolphin.sql_mode='';
            select 1 xor 1;                            
            select 1 xor 0;                    
            select '-12.3abc' xor -100.1;            
            select '-12.3abc' xor 0;            
            select '-12.3abc' xor 5;            
            select '-12.3abc' xor 158.3;            
            select '-12.3abc' xor -8.222e4;           
            select '-12.3abc' xor true;            
            select '-12.3abc' xor false;          
            select '-12.3abc' xor 'null';'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        strip_cmd = [x.strip() for x in sql_res.splitlines() if
                     x.strip() != '']
        self.log.info(strip_cmd)
        self.assertTrue(strip_cmd.count('1') == 4, '执行失败:' + text)
        self.assertTrue(strip_cmd.count('0') == 6, '执行失败:' + text)

        text = '-----step4:验证xor操作符,含null值;expect:成功，返回null---'
        self.log.info(text)
        sql_cmd = f'''select 1 xor null;           
            select null xor 1;
            select '-12.3abc' xor null;'''
        sql_res = self.sh_primy.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.splitlines()[2].strip()) == 0,
                        '执行失败:' + text)
        self.assertTrue(len(sql_res.splitlines()[7].strip()) == 0,
                        '执行失败:' + text)
        self.assertIn('WARNING:  invalid input syntax for type int16',
                      sql_res, '执行失败:' + text)
        self.assertIn('xor \n-----\n    \n(1 row)', sql_res,
                      '执行失败' + text)

    def tearDown(self):
        text = '--step5:清理环境;expect:清理环境完成--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res = self.sh_primy.execut_db_sql(sql_cmd)
        self.log.info(sql_res)

        self.log.info('断言teardown成功')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
